<?php
	/*
	 * Created by Devi Bastian
	 * 
	 */	
	 /* permission name 
	 akademik_master_kelas
	 akademik_master_kelas_add
	 akademik_master_kelas_edit
	 akademik_master_kelas_del	 
	 */
	class KelasController extends CController{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Master Akademik";
		public $subMenu = "";
		public $title = "";		
		public $loginUser;
		
		public function init() {
			$loginUser = LoginUser::getUser();	
		}
		
		public function actionIndex(){
			LoginUser::getPermission("akademik_master_kelas");
			$this->subMenu = "Kelas";
			$this->layout ="//layouts/ly_blank";
			$data=array("department_id"=>"");			
			$this->render('index',$data);
		}
		
		public function actiondetailList(){		
			$onlyActive = Yii::app()->request->getParam("activeOnly",false);
			$department_id = Yii::app()->request->getParam("department_id","");
						
			if ($onlyActive){
				$vKelas = VKelas::model()->findAll("department_id = :deptId and is_active = true order by id",array(":deptId" =>$department_id));
			}else{
				//$vKelas = VKelas::model()->findAll(array("condition"=>"department_id = :deptId","order"=>"id"),array(":deptId" =>$department_id));
				$vKelas = VKelas::model()->findAll("department_id = :deptId order by id",array(":deptId" =>$department_id));
			}			
			$this->result["actionStatus"] =0;
			$this->result["actionMessage"] ="";
			$this->result["actionError"] =array();
			$this->result["model"] = $vKelas;			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}
		
		public function actionedit(){		
			$op = Yii::app()->request->getParam("op","add");
			$status = Yii::app()->request->getParam("is_active",0) === "on"?1:0 ;			
			if ($op == "add"){
				LoginUser::getPermission("akademik_master_kelas_add");
				$kelas  = new MKelas();
				$kelas->tingkatan_id = Yii::app()->request->getParam("tingkatan_id","");
				$kelas->name = Yii::app()->request->getParam("name","");
				$kelas->keterangan = Yii::app()->request->getParam("keterangan","");
				$kelas->is_active=$status;
				if ($kelas->save()){
					echo "<script>
						alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($kelas->getErrors());
				}				
			}else if ($op == "edit"){
				LoginUser::getPermission("akademik_master_kelas_edit");
				//print_r ($_POST);
				$id = Yii::app()->request->getParam("id","");				
				$kelas = MKelas::model()->find("id=:id",array(":id"=>$id));
				$kelas->name = Yii::app()->request->getParam("name","");
				$kelas->tingkatan_id = Yii::app()->request->getParam("tingkatan_id","");
				$kelas->keterangan= Yii::app()->request->getParam("keterangan","");					
				$kelas->is_active=$status;
				if ($kelas->save()){
					echo "<script>
						alert('".Yii::t("dialog","Perubahan data sudah di simpan...!"). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($department->getErrors());
				}				
			}else if ($op == "del"){
				$errorMessage = Yii::t("dialog","Data gagal di hapus !");
				
				if (!LoginUser::getPermission("akademik_master_kelas_del",null,false)){
					$this->result["actionStatus"] =1;
					$this->result["actionMessage"] = Yii::t("alert","Anda tidak memiliki ijin untuk menghapus data ini !");
					$this->result["actionError"] =array();
					$this->result["model"] = array();
					header('Content-type:application/json');
					echo CJSON::encode($this->result);
					return;
				}
				$id = Yii::app()->request->getParam("id","");
				$conn = Yii::app()->db;
				$sql = "delete from m_kelas where id = :id";
				$command = $conn->createCommand($sql);
				try{
					//$command->bindParam(":id",$id,PDO::PARAM_INT);
					if ($command->execute(array(":id"=>$id))){
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="";
						$this->result["actionError"] =array();
						$this->result["model"] = array();
					}
				}catch(Exception $e){
					$this->result["actionStatus"] =2;
					$this->result["actionMessage"] = $errorMessage;
					$this->result["actionError"] =$e->getMessage();
					$this->result["model"] = array();
				}
				Yii::app()->db->active = false;
			}
			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}
		
		public function actionform(){
			$op = Yii::app()->request->getParam("op","add");
			$callback = Yii::app()->request->getParam("callback","");
			$department_id = Yii::app()->request->getParam("department_id","");
			
			$data = array("op"=>"add","callback"=>"","id"=>0,"department_id" =>$department_id,"name"=>"","tingkatan_id"=>"","keterangan"=>"","is_active"=>"1");
			
			if ($op == "add"){
				LoginUser::getPermission("akademik_master_kelas_add");
				$data["op"] = "add";
				$data["callback"] = $callback;				
			}else if($op =="edit"){				
				LoginUser::getPermission("akademik_master_kelas_edit");			
				$data["op"] = "edit";
				$data["callback"] = $callback;				
				$id = Yii::app()->request->getParam("id","0");				
				$data["id"] = $id;				
				$kelas = MKelas::model()->findAll("id = :id order by id",array("id"=>$id));
								
				if (count($kelas) > 0){
					$tingkatan = $kelas[0]->tingkatan;
					$data["department_id"] = $tingkatan["department_id"];
					$data["name"] = $kelas[0]->name;
					$data["tingkatan_id"] = $kelas[0]->tingkatan_id;
					$data["keterangan"] = $kelas[0]->keterangan;					
					$data["is_active"] = $kelas[0]->is_active;
				}				
			}			
			$this->layout ="//layouts/ly_blank";
			$this->render("form",$data);
		}
				
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
	}
?>